import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.CommentGeneratorConfiguration;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.config.JavaClientGeneratorConfiguration;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.config.ModelType;
import org.mybatis.generator.config.SqlMapGeneratorConfiguration;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MybatisGenerator {

  public static void main(String[] args) throws Exception {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    Configuration config = new Configuration();
    config.addContext(getContext());

    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
  }

  private static Context getContext() {
    Context context = new Context(ModelType.CONDITIONAL);
    context.setId("mysql");
    context.setTargetRuntime("MyBatis3Simple");

    CommentGeneratorConfiguration commentConf = new CommentGeneratorConfiguration();
    commentConf.addProperty("suppressAllComments", "true");
    commentConf.addProperty("suppressDate", "true");
    context.setCommentGeneratorConfiguration(commentConf);

    context.setJdbcConnectionConfiguration(getJDBCConnectionConfiguration());
    // 数据表对应的 model 的配置
    JavaModelGeneratorConfiguration conf = new JavaModelGeneratorConfiguration();
    conf.setTargetProject("mybatis/src/main/java");
    conf.setTargetPackage("com.bpzj.mybatis.model");
    conf.addProperty("trimStrings", "true");
    context.setJavaModelGeneratorConfiguration(conf);

    // mapper接口的配置
    JavaClientGeneratorConfiguration configuration = new JavaClientGeneratorConfiguration();
    configuration.setTargetProject("mybatis/src/main/java");
    configuration.setTargetPackage("com.bpzj.mybatis.mapper");
    configuration.setConfigurationType("XMLMAPPER");

    SqlMapGeneratorConfiguration sqlMapConf = new SqlMapGeneratorConfiguration();
    sqlMapConf.setTargetProject("mybatis/src/main/resources");
    sqlMapConf.setTargetPackage("com.bpzj.mybatis.mapper");

    context.setJavaClientGeneratorConfiguration(configuration);
    context.setSqlMapGeneratorConfiguration(sqlMapConf);

    TableConfiguration table = new TableConfiguration(context);
    table.setTableName("blog");
    context.addTableConfiguration(table);

    return context;
  }


  private static JDBCConnectionConfiguration getJDBCConnectionConfiguration() {
    JDBCConnectionConfiguration jdbcConnection = new JDBCConnectionConfiguration();
    jdbcConnection.setDriverClass("com.mysql.cj.jdbc.Driver");
    jdbcConnection.setConnectionURL("jdbc:mysql://192.168.137.10:3306/test");
    jdbcConnection.setUserId("root");
    jdbcConnection.setPassword("minmin");
    return jdbcConnection;
  }

}
